{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "# Python Version of Tree SHAP\n", "\n", "This is a sample implementation of Tree SHAP written in Python for easy reading." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2023-10-14T15:13:45.176673980Z", "start_time": "2023-10-14T15:13:42.734552921Z" }, "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "c:\\programming\\shap\\.venv\\Lib\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", " from .autonotebook import tqdm as notebook_tqdm\n" ] } ], "source": [ "import time\n", "\n", "import numba\n", "import numpy as np\n", "import pandas as pd\n", "import sklearn.ensemble\n", "import xgboost\n", "\n", "import shap" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "### Load California dataset" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2023-10-14T15:13:45.191494227Z", "start_time": "2023-10-14T15:13:45.179770187Z" }, "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(1000, 8)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X, y = shap.datasets.california(n_points=1000)\n", "X.shape" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "### Train sklearn random forest" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2023-10-14T15:13:46.873411331Z", "start_time": "2023-10-14T15:13:45.219992128Z" }, "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
RandomForestRegressor(max_depth=4, n_estimators=1000)In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.